package com.example.ncpapplication.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.ncpapplication.R;
import com.example.ncpapplication.bean.NCPInfo;

import java.util.List;

public class MyAdapter extends ArrayAdapter<NCPInfo.NewslistBean> {

    private int resource;

    public MyAdapter(@NonNull Context context, int resource, @NonNull List<NCPInfo.NewslistBean> objects) {
        super(context, resource, objects);
        this.resource = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        NCPInfo.NewslistBean province = getItem(position);
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(resource, parent, false);
            holder = new ViewHolder();
            holder.tv_item_provinceName = convertView.findViewById(R.id.tv_item_provinceName);
            holder.tv_item_confirmedCount = convertView.findViewById(R.id.tv_item_confirmedCount);
            holder.tv_item_nowConfirmedCount = convertView.findViewById(R.id.tv_item_nowConfirmedCount);
            holder.tv_item_suspectedCount = convertView.findViewById(R.id.tv_item_suspectedCount);
            holder.tv_item_curedCount = convertView.findViewById(R.id.tv_item_curedCount);
            holder.tv_item_deadCount = convertView.findViewById(R.id.tv_item_deadCount);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_item_provinceName.setText(province.getProvinceName());
        holder.tv_item_confirmedCount.setText(province.getConfirmedCount() + "人");
        holder.tv_item_nowConfirmedCount.setText(province.getCurrentConfirmedCount() + "人");
        holder.tv_item_suspectedCount.setText(province.getSuspectedCount() + "人");
        holder.tv_item_curedCount.setText(province.getCuredCount() + "人");
        holder.tv_item_deadCount.setText(province.getDeadCount() + "人");
        return convertView;
    }

    private class ViewHolder {
        TextView tv_item_provinceName, tv_item_confirmedCount, tv_item_nowConfirmedCount, tv_item_suspectedCount, tv_item_curedCount, tv_item_deadCount;
    }

}
